<template>
  <div class="home-container">
    <!-- 顶部导航 -->
    <div class="topbar">
      <div class="logo">AI 工具</div>
      <div class="title">AI Agent 聚合</div>
      <div class="tagline">发现与使用适合你的智能体</div>
      <div class="spacer"></div>
      
      <el-dropdown @command="handleCommand">
        <div class="user-info">
          <el-avatar :size="32">{{ userStore.userInfo?.nickname?.[0] || 'U' }}</el-avatar>
          <span class="username">{{ userStore.userInfo?.nickname || '用户' }}</span>
          <el-icon><arrow-down /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人中心</el-dropdown-item>
            <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    
    <!-- 主内容 -->
    <div class="main-content">
      <!-- 左侧分类 -->
      <aside class="sidebar">
        <div class="category-list">
          <div
            v-for="cat in categories"
            :key="cat.id"
            class="category-item"
            :class="{ active: selectedCategory === cat.id }"
            @click="selectedCategory = cat.id"
          >
            {{ cat.name }}
          </div>
        </div>
      </aside>
      
      <!-- 中间Agent列表 -->
      <main class="content">
        <div class="agent-grid">
          <div
            v-for="agent in filteredAgents"
            :key="agent.id"
            class="agent-card"
            @click="goToChat(agent.id)"
          >
            <div class="agent-header">
              <div class="agent-icon" :style="{ background: getIconColor(agent.id) }">
                {{ agent.name[0] }}
              </div>
              <div class="agent-title">{{ agent.name }}</div>
            </div>
            <div class="agent-tags">
              <span v-for="(tag, i) in (agent.params?.tags || [])" :key="i" class="tag">
                {{ tag }}
              </span>
            </div>
            <div class="agent-desc">{{ truncateDesc(agent.description) }}</div>
          </div>
        </div>
        
        <div v-if="loading" class="loading">
          <el-icon class="is-loading"><loading /></el-icon>
        </div>
      </main>
      
      <!-- 右侧热门工具 -->
      <aside class="hot-tools">
        <div class="section-title">热门工具</div>
        <div class="tool-list">
          <div
            v-for="tool in hotTools"
            :key="tool.id"
            class="tool-item"
            @click="goToChat(tool.id)"
          >
            <div class="tool-icon" :style="{ background: getIconColor(tool.id) }">
              {{ tool.icon || tool.name[0] }}
            </div>
            <div class="tool-name">{{ tool.name }}</div>
          </div>
        </div>
      </aside>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown, Loading } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { getCategories, getAgents, getHotTools } from '@/api/agent'
import { logout as logoutApi } from '@/api/auth'

const router = useRouter()
const userStore = useUserStore()

const categories = ref([])
const agents = ref([])
const hotTools = ref([])
const selectedCategory = ref('all')
const loading = ref(false)

const iconColors = [
  '#eff6ff',
  '#e0e7ff',
  '#dbeafe',
  '#f0f9ff',
  '#eaf2ff'
]

const getIconColor = (id) => {
  return iconColors[id % iconColors.length]
}

const truncateDesc = (desc) => {
  if (!desc) return ''
  return desc.length > 12 ? desc.slice(0, 12) + '...' : desc
}

const filteredAgents = computed(() => {
  if (selectedCategory.value === 'all') {
    return agents.value
  }
  return agents.value.filter(a => a.category_id === selectedCategory.value)
})

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'logout') {
    try {
      await logoutApi()
      userStore.logout()
      router.push('/login')
    } catch (error) {
      console.error(error)
    }
  }
}

const goToChat = (agentId) => {
  router.push(`/chat/${agentId}`)
}

const loadCategories = async () => {
  try {
    const res = await getCategories()
    categories.value = [{ id: 'all', name: '全部' }, ...res.data]
  } catch (error) {
    console.error(error)
  }
}

const loadAgents = async () => {
  try {
    loading.value = true
    const res = await getAgents()
    agents.value = res.data.items || []
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

const loadHotTools = async () => {
  try {
    const res = await getHotTools()
    hotTools.value = res.data || []
  } catch (error) {
    console.error(error)
  }
}

onMounted(() => {
  loadCategories()
  loadAgents()
  loadHotTools()
})
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: #2a5085;
  color: #e6edf7;
  border-bottom: 1px solid rgba(147, 197, 253, 0.6);
}

.logo {
  font-weight: 700;
  letter-spacing: 0.4px;
}

.title {
  font-weight: 600;
}

.tagline {
  color: #94a3b8;
  font-size: 12px;
}

.spacer {
  flex: 1;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 999px;
  transition: background 0.2s;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.1);
}

.username {
  font-size: 14px;
}

.main-content {
  display: flex;
  gap: 12px;
  padding: 12px;
  flex: 1;
}

.sidebar {
  width: 220px;
  background: #2a5085;
  border-radius: 14px;
  padding: 12px;
  color: #e6edf7;
}

.category-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.category-item {
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid rgba(147, 197, 253, 0.45);
}

.category-item:hover {
  background: rgba(59, 130, 246, 0.06);
}

.category-item.active {
  background: rgba(59, 130, 246, 0.18);
  border-color: rgba(147, 197, 253, 0.6);
}

.content {
  flex: 1;
  background: #2a5085;
  border-radius: 14px;
  padding: 12px;
  overflow-y: auto;
}

.agent-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.agent-card {
  background: #2e5f97;
  border: 1px solid rgba(147, 197, 253, 0.6);
  border-radius: 14px;
  padding: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.agent-card:hover {
  border-color: rgba(147, 197, 253, 0.6);
  background: rgba(59, 130, 246, 0.06);
}

.agent-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.agent-icon {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #93c5fd;
  border: 1px solid rgba(147, 197, 253, 0.55);
}

.agent-title {
  font-size: 15px;
  font-weight: 500;
  color: #e6edf7;
}

.agent-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.tag {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(59, 130, 246, 0.12);
  color: #93c5fd;
  border: 1px solid rgba(147, 197, 253, 0.55);
}

.agent-desc {
  color: #e6edf7;
  font-size: 13px;
}

.hot-tools {
  width: 280px;
  background: #2a5085;
  border-radius: 14px;
  overflow: hidden;
  color: #e6edf7;
}

.section-title {
  font-size: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(147, 197, 253, 0.55);
}

.tool-list {
  padding: 8px;
}

.tool-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(147, 197, 253, 0.55);
  cursor: pointer;
  transition: background 0.15s;
}

.tool-item:hover {
  background: rgba(59, 130, 246, 0.05);
}

.tool-icon {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: #93c5fd;
  border: 1px solid rgba(147, 197, 253, 0.55);
}

.tool-name {
  font-size: 14px;
}

.loading {
  text-align: center;
  padding: 20px;
  color: #94a3b8;
}
</style>

